package dump;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Contacts;

public class Phone {

	public static class PhoneCollection extends DatabaseCollection {
		public PhoneCollection(final Cursor cursor) {
			super(cursor);
		}

		@Override
		public ContentValues cursorToValues(final Cursor cursor) {
			return Phone.cursorToPhoneValues(cursor);
		}
	}

	static final String[] phonesProjection = new String[] {
			android.provider.BaseColumns._ID,
			android.provider.Contacts.PhonesColumns.LABEL,
			android.provider.Contacts.PhonesColumns.NUMBER,
			android.provider.Contacts.PhonesColumns.NUMBER_KEY,
			android.provider.Contacts.PhonesColumns.TYPE };

	public static void addPhone(final ContentResolver resolver,
			final ContentValues phone, final String userId) {
		final Uri peopleUri = Uri.withAppendedPath(Contacts.People.CONTENT_URI,
				userId);
		resolver.insert(Uri.withAppendedPath(peopleUri,
				Contacts.People.Phones.CONTENT_DIRECTORY), phone);
	}

	private static ContentValues cursorToPhoneValues(final Cursor cursor) {
		if (cursor == null) {
			return null;
		}

		final ContentValues values = new ContentValues();
		String val;
		val = cursor.getString(cursor
				.getColumnIndex(android.provider.BaseColumns._ID));
		values.put(android.provider.BaseColumns._ID, val);

		val = cursor.getString(cursor
				.getColumnIndex(Contacts.PhonesColumns.LABEL));
		values.put(Contacts.PhonesColumns.LABEL, val);

		val = cursor.getString(cursor
				.getColumnIndex(Contacts.PhonesColumns.NUMBER));
		values.put(Contacts.PhonesColumns.NUMBER, val);

		final Integer intVal = new Integer(cursor.getInt(cursor
				.getColumnIndex(Contacts.PhonesColumns.TYPE)));
		values.put(Contacts.PhonesColumns.TYPE, intVal);

		return values;
	}

	public static void deletePhone(final ContentResolver resolver,
			final String phoneId, final String userId) {
		resolver.delete(Uri.withAppendedPath(Contacts.Phones.CONTENT_URI,
				phoneId), null, null);
	}

	/**
	 * getPhones
	 * 
	 * Get the phone numbers for a Contact.
	 * 
	 * @param resolver
	 * @param userId
	 *            the Contact whose phones to return
	 * @return
	 */
	public static PhoneCollection getPhones(final ContentResolver resolver,
			final String userId) {
		if (userId == null) {
			return null;
		}

		final String[] whereArgs = new String[] { userId };
		return new PhoneCollection(resolver.query(Contacts.Phones.CONTENT_URI,
				Phone.phonesProjection, "people."
						+ android.provider.BaseColumns._ID + " = ?", whereArgs,
				Contacts.PhonesColumns.TYPE + " ASC"));
	}

	public static void savePhone(final ContentResolver resolver,
			final ContentValues phone, final String phoneId, final String userId) {
		final Uri uri = Uri.withAppendedPath(Contacts.Phones.CONTENT_URI,
				phoneId);
		resolver.update(uri, phone, null, null);
	}
}
